package cn.yunyichina.provider.base.service.impl;

import cn.yunyichina.provider.base.dao.CommentDao;
import cn.yunyichina.provider.base.dao.OrderDao;
import cn.yunyichina.provider.base.entity.Comment;
import cn.yunyichina.provider.base.entity.Order;
import cn.yunyichina.provider.base.vo.GetCommnetNotOrderVo;
import cn.yunyichina.provider.framework.annotation.validate.ValidateException;
import cn.yunyichina.provider.framework.annotation.validate.Validator;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.utils.commons.DateUtils;
import com.alibaba.fastjson.JSON;
import org.apache.tools.ant.taskdefs.Get;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by kevin on 2017/9/14.
 */
@Service
public class CommentServiceImpl {

    private static final Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);

    @Autowired
    CommentDao commentDao;

    @Autowired
    OrderDao orderDao;

    /**
     * 新增就诊评价
     * yyt.base.comment.add
     * 入参：orderNo,mobile,username,service,comment,quality
     * @param data
     * @return
     */
    public Response addComment(String data) {
        Response response = new Response();
        response.setResultCodeAndMessage("1","add fail");
        Comment comment= JSON.parseObject(data,Comment.class);

        // 校验RequestVo的数据h
        try {
            Validator.getInstance().validate(comment);
        } catch (ValidateException ex) {
            logger.error(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！",ex);
            response.setResultCodeAndMessage("1001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }

        comment.setId(PKGenerator.generateId());
        comment.setCreatedTime(DateUtils.dateToString(new Date()));
        commentDao.insert(comment);

        Order wheres=new Order();
        wheres.setOrderNo(comment.getOrderNo());
        List<Order> orders=orderDao.select(wheres);
        if(orders.size()==0){
            response.setResultCodeAndMessage("1001", "不存在该订单，请检查");
            return response;
        }

        Order updateOrder=new Order();
        updateOrder.setId(orders.get(0).getId());
        updateOrder.setCommentFlag(1);
        orderDao.update(updateOrder);

        response.setResultCodeAndMessage("0","新增成功");
        return response;
    }

    /**
     * 获取当前用户，当前医院，未就诊评价的订单
     * yyt.base.comment.not.order.get
     * 入参:openId,hospitalCode
     * @param data
     * @return
     */
    public ResponseObject getCommentNotOrder(String data){
        ResponseObject response = new ResponseObject();
        response.setResultCodeAndMessage("1","get fail");
        GetCommnetNotOrderVo getCommnetNotOrderVo=JSON.parseObject(data, GetCommnetNotOrderVo.class);

        try {
            Validator.getInstance().validate(getCommnetNotOrderVo);
        } catch (ValidateException ex) {
            logger.error(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！",ex);
            response.setResultCodeAndMessage("1001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }

        Order wheres=new Order();
        wheres.setOpenId(getCommnetNotOrderVo.getOpenId());
        wheres.setOrgCode(getCommnetNotOrderVo.getHospitalCode());
        wheres.setCommentFlag(0);
        wheres.setBeginCreatedTime(DateUtils.lastMonthDate());
        wheres.setEndCreatedTime(DateUtils.getDateTimeStr());
        List<Order> orders=orderDao.select(wheres);

        response.setResultCodeAndMessageAndResult("0","查询成功",orders);
        return response;
    }


}
